package com.cp_smss.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cp_smss.common.GlobalConstants;
import com.cp_smss.model.DWZResponser;
import com.cp_smss.model.Department;
import com.cp_smss.model.PageModel;
import com.cp_smss.model.User;
import com.cp_smss.service.DepartmentService;
import com.cp_smss.service.UserService;
import com.cp_smss.util.DWZResponseFactory;

@Controller
@RequestMapping(value="/user/")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private DepartmentService departmentService;
	
	private int pageNo = 1;
	private int pageSize = 10;
	
	@RequestMapping(value="add")
	@ResponseBody
	public Object addUser(User user) throws BindException{
		int result = userService.addUser(user);
		
		DWZResponser dwzResponser = DWZResponseFactory.create();
		dwzResponser.setStatusCode(1==result?GlobalConstants.AJAX_SUCCESS:GlobalConstants.AJAX_FAIL);
		dwzResponser.setMessage(GlobalConstants.SUCCESS_OPERATE);
		dwzResponser.setCallbackType(GlobalConstants.AJAX_CALLBACK_CLOSE_CURRENT);
		dwzResponser.setNavTabId("userList");
		return dwzResponser;
	}
	
	@RequestMapping(value="update/{userId}",method=RequestMethod.GET)
	public String preUpdateUser(@PathVariable String userId,Model model){
		List<Department> departments = departmentService.getDepartments();
		model.addAttribute("departments", departments);
		User user = userService.getUserById(userId);
		model.addAttribute("user", user);
		return "user/update";
	}
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public DWZResponser updateUser(User user){
		int result = userService.updateUser(user);
		
		DWZResponser dwzResponser = DWZResponseFactory.create();
		dwzResponser.setStatusCode(1==result?GlobalConstants.AJAX_SUCCESS:GlobalConstants.AJAX_FAIL);
		dwzResponser.setMessage(GlobalConstants.SUCCESS_OPERATE);
		dwzResponser.setCallbackType(GlobalConstants.AJAX_CALLBACK_CLOSE_CURRENT);
		dwzResponser.setNavTabId("userList");
		return dwzResponser;
	}

	@RequestMapping(value="del/{userId}")
	public String delUser(@PathVariable String userId,HttpServletRequest request){
		
		userService.delUser(userId);
		return "";
	}
	
	@RequestMapping(value="/list")
	public String listUser(PageModel<User> pageModel){
		if(pageModel.getPageNo()!=0){
			pageNo=pageModel.getPageNo();
		}
		if(pageModel.getPageSize()!=10){
			pageSize=pageModel.getPageSize();
		}
		pageModel = userService.listUsers(pageNo,pageSize);
		return "listUser";
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
}
